package com.lanren.familybookapp.ui.home.model;

import com.lanren.common.app.ApiUrl;
import com.lanren.common.base.impl.BaseModel;
import com.lanren.common.http.HttpManager;
import com.lanren.common.utils.GsonUtils;
import com.lanren.common.utils.LogUtils;
import com.lanren.common.utils.StringUtils;
import com.lanren.easyhttp.callback.SimpleCallBack;
import com.lanren.easyhttp.exception.ApiException;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.app.UserInfoBean;
import com.lanren.familybookapp.ui.home.bean.BookListBean;
import com.lanren.familybookapp.ui.home.interfaces.IHomeModel;
import com.lanren.familybookapp.ui.me.bean.BookInfoBean;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * @ClassName HomeModel
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/5 16:58
 * @Version 1.0
 */
public class HomeModel extends BaseModel {
    public HomeModel() {
        initDispoable();
    }

    public void getBookList(int bookId, String bookTime, int page,
                            String member, final IHomeModel.HomeModel homeModel) {

        Map<String, String> map = new LinkedHashMap<>();
        map.put("bookId", bookId + "");
        map.put("page", page + "");
        map.put("bookTime", bookTime);
        if (StringUtils.isNotBlank(member)) {
            map.put("member", member);
        }
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_BOOKKEEPING_GET)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<List<BookListBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        homeModel.onError(e.getCode(), e.getMessage());
                    }

                    @Override
                    public void onSuccess(List<BookListBean> bookListBeans) {
                        homeModel.onSuccess(bookListBeans);
                    }

                });
        addDisposable(disposable);
    }

    //获取账本详细
    public void getBookInfo(int bookId, final IHomeModel.GetBookInfoModel getBookInfoModel) {

        Map<String, String> map = new LinkedHashMap<>();
        map.put("bookId", bookId + "");
        Disposable disposable = HttpManager.post(ApiUrl.API.BOOK_MANAGER_LIST)
                .headers(AppUserHelper.getHttpHeaders())
                .params(map)
                .execute(new SimpleCallBack<List<BookInfoBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        getBookInfoModel.onError(e.getCode(), e.getMessage());
                    }

                    @Override
                    public void onSuccess(List<BookInfoBean> bookInfoBeans) {
                        getBookInfoModel.onSuccess(bookInfoBeans);

                    }
                });
        addDisposable(disposable);

    }

    public void getUserInfo(final IHomeModel.MainUserInfoModel meModel) {
        Disposable disposable = HttpManager.post(ApiUrl.API.USER_INFO_GET)
                .headers(AppUserHelper.getHttpHeaders())
                .execute(new SimpleCallBack<UserInfoBean>() {
                    @Override
                    public void onError(ApiException e) {
                        meModel.onError(e.getCode(), e.getMessage());
                    }

                    @Override
                    public void onSuccess(UserInfoBean userInfoBean) {
                        AppUserHelper.getInstance().setUserInfoBean(userInfoBean);

                        LogUtils.d("USER_INFO_GET=" + GsonUtils.toJson(userInfoBean));
                        meModel.onSuccess();
                    }
                });
        addDisposable(disposable);
    }
}
